// ============================== 导入依赖 ============================== //

// ============================== 导出工具 ============================== //

export default {
  /**
   * 获取结构树数据
   *
   * @param {Array} treeData 结构树数据
   * @param {Array} treeDict 结构树字典
   * @param {Array} grayed 置灰列表
   */
  getTreeData(treeData, treeDict, grayed) {
    treeDict.forEach(([children, id, name]) => {
      const node = { label: name, value: id, disabled: grayed.includes(id) }
      treeData.push(node)

      if (children.length) {
        node.children = []
        this.getTreeData(node.children, children, grayed)
      }
    })
    return treeData
  },

  /**
   * 获取 ID 列表的映射
   *
   * @param {Object} idListMap ID 列表的映射
   * @param {Array} treeDict 结构树字典
   * @param {Array} idList ID 列表
   */
  getIdListMap(idListMap, treeDict, idList = []) {
    treeDict.forEach(([children, id]) => {
      const newIdList = [...idList, id]
      idListMap[id] = newIdList
      if (children.length) this.getIdListMap(idListMap, children, newIdList)
    })
    return idListMap
  },

  /**
   * 获取名称列表的映射
   *
   * @param {Object} nameListMap 名称列表的映射
   * @param {Array} treeDict 结构树字典
   * @param {Array} nameList 名称列表
   */
  getNameListMap(nameListMap, treeDict, nameList = []) {
    treeDict.forEach(([children, id, name]) => {
      const newNameList = [...nameList, name]
      nameListMap[id] = newNameList
      if (children.length) this.getNameListMap(nameListMap, children, newNameList)
    })
    return nameListMap
  },

  /**
   * 获取结点字典
   *
   * @param {Array} nodeDict 结点字典
   * @param {Array} treeDict 结构树字典
   */
  getNodeDict(nodeDict, treeDict) {
    treeDict.forEach(([children, ...node]) => {
      nodeDict.push(node)
      if (children.length) this.getNodeDict(nodeDict, children)
    })
    return nodeDict
  },

  /**
   * 获取结点映射
   *
   * @param {Array} nodeMap 结点映射
   * @param {Array} treeDict 结构树字典
   */
  getNodeMap(nodeMap, treeDict) {
    treeDict.forEach((node) => {
      nodeMap[node[1]] = node
      if (node[0].length) this.getNodeMap(nodeMap, node[0])
    })
    return nodeMap
  },

  /**
   * 获取首个叶子 ID 列表
   *
   * @param {Array} idList ID 列表
   * @param {Array} treeDict 结构树字典
   */
  getFirstIdList(idList, treeDict) {
    if (treeDict && treeDict.length) {
      const [children, id] = treeDict[0]
      idList.push(id)
      this.getFirstIdList(idList, children)
    }
    return idList
  },
}
